package cn.teaseed.view;

import cn.hutool.core.util.StrUtil;
import cn.teaseed.comn.CorsParam;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@Slf4j
@Component
public class WaveView implements Initializable {


    public ComboBox<String> waveName;
    public Text pulseFrequency;
    public Slider pulseFrequencySlider;
    public Slider shape1;
    public Slider shape2;
    public Slider shape3;
    public Slider shape4;
    public Slider shape5;
    public Slider shape6;
    public Slider shape7;
    public Slider shape8;
    public Slider shape9;
    public Slider shape10;
    public Slider shape11;
    public Text shape1Num;
    public Text shape2Num;
    public Text shape3Num;
    public Text shape4Num;
    public Text shape5Num;
    public Text shape6Num;
    public Text shape7Num;
    public Text shape8Num;
    public Text shape9Num;
    public Text shape10Num;
    public Text shape11Num;
    public Button saveButton;
    public Button backButton;
    public VBox outVbox;

    @Resource
    private HomeView homeView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //加载波形名称
        waveName.getItems().setAll(CorsParam.WaveMap.keySet());

        pulseFrequencySlider.valueProperty().addListener((observable, oldValue, newValue) ->
                pulseFrequency.setText(String.valueOf(newValue.intValue())));
        shape1.valueProperty().addListener((observable, oldValue, newValue) ->
                shape1Num.setText(String.valueOf(newValue.intValue())));
        shape2.valueProperty().addListener((observable, oldValue, newValue) ->
                shape2Num.setText(String.valueOf(newValue.intValue())));
        shape3.valueProperty().addListener((observable, oldValue, newValue) ->
                shape3Num.setText(String.valueOf(newValue.intValue())));
        shape4.valueProperty().addListener((observable, oldValue, newValue) ->
                shape4Num.setText(String.valueOf(newValue.intValue())));
        shape5.valueProperty().addListener((observable, oldValue, newValue) ->
                shape5Num.setText(String.valueOf(newValue.intValue())));
        shape6.valueProperty().addListener((observable, oldValue, newValue) ->
                shape6Num.setText(String.valueOf(newValue.intValue())));
        shape7.valueProperty().addListener((observable, oldValue, newValue) ->
                shape7Num.setText(String.valueOf(newValue.intValue())));
        shape8.valueProperty().addListener((observable, oldValue, newValue) ->
                shape8Num.setText(String.valueOf(newValue.intValue())));
        shape9.valueProperty().addListener((observable, oldValue, newValue) ->
                shape9Num.setText(String.valueOf(newValue.intValue())));
        shape10.valueProperty().addListener((observable, oldValue, newValue) ->
                shape10Num.setText(String.valueOf(newValue.intValue())));
        shape11.valueProperty().addListener((observable, oldValue, newValue) ->
                shape11Num.setText(String.valueOf(newValue.intValue())));
    }

    public void loadWave() {
        String name = waveName.getEditor().getText();
        if (StrUtil.isNotEmpty(name) && CorsParam.WaveMap.get(name) != null) {
            List<String> wave = CorsParam.WaveMap.get(name);
            String frequency = StrUtil.split(wave.get(0), 2)[0];
            Integer frequencyNum = Integer.valueOf(frequency, 16);
            pulseFrequency.setText(frequencyNum.toString());
            pulseFrequencySlider.setValue(frequencyNum.doubleValue());

            String shape1Hex = StrUtil.split(StrUtil.split(wave.get(0), 8)[1], 2)[0];
            String shape2Hex = StrUtil.split(StrUtil.split(wave.get(1), 8)[1], 2)[0];
            String shape3Hex = StrUtil.split(StrUtil.split(wave.get(2), 8)[1], 2)[0];
            String shape4Hex = StrUtil.split(StrUtil.split(wave.get(3), 8)[1], 2)[0];
            String shape5Hex = StrUtil.split(StrUtil.split(wave.get(4), 8)[1], 2)[0];
            String shape6Hex = StrUtil.split(StrUtil.split(wave.get(5), 8)[1], 2)[0];
            String shape7Hex = StrUtil.split(StrUtil.split(wave.get(6), 8)[1], 2)[0];
            String shape8Hex = StrUtil.split(StrUtil.split(wave.get(7), 8)[1], 2)[0];
            String shape9Hex = StrUtil.split(StrUtil.split(wave.get(8), 8)[1], 2)[0];
            String shape10Hex = StrUtil.split(StrUtil.split(wave.get(9), 8)[1], 2)[0];
            String shape11Hex = StrUtil.split(StrUtil.split(wave.get(10), 8)[1], 2)[0];
            shape1.setValue(Integer.valueOf(shape1Hex, 16).doubleValue());
            shape2.setValue(Integer.valueOf(shape2Hex, 16).doubleValue());
            shape3.setValue(Integer.valueOf(shape3Hex, 16).doubleValue());
            shape4.setValue(Integer.valueOf(shape4Hex, 16).doubleValue());
            shape5.setValue(Integer.valueOf(shape5Hex, 16).doubleValue());
            shape6.setValue(Integer.valueOf(shape6Hex, 16).doubleValue());
            shape7.setValue(Integer.valueOf(shape7Hex, 16).doubleValue());
            shape8.setValue(Integer.valueOf(shape8Hex, 16).doubleValue());
            shape9.setValue(Integer.valueOf(shape9Hex, 16).doubleValue());
            shape10.setValue(Integer.valueOf(shape10Hex, 16).doubleValue());
            shape11.setValue(Integer.valueOf(shape11Hex, 16).doubleValue());
        }
    }

    public void closeWindow(ActionEvent actionEvent) {
        Stage window = (Stage) backButton.getScene().getWindow();
        window.close();
    }

    public void saveWave(ActionEvent actionEvent) {
        String name = waveName.getEditor().getText();

        //取值
        int frequency = (int) pulseFrequencySlider.getValue();
        int shape1Num = (int) shape1.getValue();
        int shape2Num = (int) shape2.getValue();
        int shape3Num = (int) shape3.getValue();
        int shape4Num = (int) shape4.getValue();
        int shape5Num = (int) shape5.getValue();
        int shape6Num = (int) shape6.getValue();
        int shape7Num = (int) shape7.getValue();
        int shape8Num = (int) shape8.getValue();
        int shape9Num = (int) shape9.getValue();
        int shape10Num = (int) shape10.getValue();
        int shape11Num = (int) shape11.getValue();
        //转换16进制
        String frequencyHex = String.format("%02X", frequency);
        String shape1Hex = String.format("%02X", shape1Num);
        String shape2Hex = String.format("%02X", shape2Num);
        String shape3Hex = String.format("%02X", shape3Num);
        String shape4Hex = String.format("%02X", shape4Num);
        String shape5Hex = String.format("%02X", shape5Num);
        String shape6Hex = String.format("%02X", shape6Num);
        String shape7Hex = String.format("%02X", shape7Num);
        String shape8Hex = String.format("%02X", shape8Num);
        String shape9Hex = String.format("%02X", shape9Num);
        String shape10Hex = String.format("%02X", shape10Num);
        String shape11Hex = String.format("%02X", shape11Num);

        //波形频率
        String hexStart = repeatString(frequencyHex, 4);
        //频率列表
        List<String> hexList = new ArrayList<String>() {
            {
                add(hexStart + repeatString(shape1Hex, 4));
                add(hexStart + repeatString(shape2Hex, 4));
                add(hexStart + repeatString(shape3Hex, 4));
                add(hexStart + repeatString(shape4Hex, 4));
                add(hexStart + repeatString(shape5Hex, 4));
                add(hexStart + repeatString(shape6Hex, 4));
                add(hexStart + repeatString(shape7Hex, 4));
                add(hexStart + repeatString(shape8Hex, 4));
                add(hexStart + repeatString(shape9Hex, 4));
                add(hexStart + repeatString(shape10Hex, 4));
                add(hexStart + repeatString(shape11Hex, 4));
            }
        };

        CorsParam.WaveMap.put(name,hexList);
        homeView.refreshJob();

        Stage window = (Stage) saveButton.getScene().getWindow();
        window.close();

    }

    public static String repeatString(String str, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(str);
        }
        return sb.toString();
    }
}
